package it.poste.il.compensatore.dao.impl;

import java.util.List;
import java.util.Properties;

import javax.persistence.Query;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import it.poste.il.compensatore.dao.SDPRequestDao;
import it.poste.il.compensatore.entities.SDPRequest;
import it.poste.il.compensatore.utilities.LoadQueriesSQL;

public class SDPRequestDaoImpl extends BaseEntityDaoImpl<SDPRequest> implements SDPRequestDao {
    public SDPRequestDaoImpl() {
        super(SDPRequest.class, "SDPRequest");
    }

    LoadQueriesSQL loadQueriesSQL;
    
	public LoadQueriesSQL getLoadQueriesSQL() {
		return loadQueriesSQL;
	}

	public void setLoadQueriesSQL(LoadQueriesSQL loadQueriesSQL) {
		this.loadQueriesSQL = loadQueriesSQL;
	}

	/**
	 * Take the list of SDP Requests from an SDP Request as filter.
	 * @param entityFilter Entity SDP Request uses as filter.
	 * @return List The List of SDP Requests.
	 */
    @SuppressWarnings("unchecked")
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public List<SDPRequest> loadByFilter(SDPRequest entityFilter) {
    	Properties p = loadQueriesSQL.getProperties();
		String querySQL = p.getProperty("loadSDPRequestByFilter");
		Query query = getEntityManager().createNativeQuery(querySQL, SDPRequest.class);
		query.setParameter("ACCOUNTINGDATE", entityFilter.getAccountingDate());
		query.setParameter("branch", entityFilter.getBranch());
		query.setParameter("numberoperationpdl", entityFilter.getNumberOperationPdl());
		query.setParameter("workingstationid", entityFilter.getWorkingStationId());
		List<SDPRequest> sdpRequests = query.getResultList();
		return sdpRequests;
	}

	/**
	 * Take the SDP Requests from an SDP Request as filter.
	 * @param entityFilter Entity SDP Request uses as filter.
	 * @return SDPRequest The Entity SDP Request founded.
	 */
	@Transactional
	public SDPRequest loadByReq(SDPRequest entityFilter) {
    	Properties p = loadQueriesSQL.getProperties();
		String querySQL = p.getProperty("loadSDPRequestByReq");
		Query query = getEntityManager().createNativeQuery(querySQL, SDPRequest.class);
		query.setParameter("ACCOUNTINGDATE", entityFilter.getAccountingDate());
		query.setParameter("branch", entityFilter.getBranch());
		query.setParameter("numberoperationpdl", entityFilter.getNumberOperationPdl());
		query.setParameter("workingstationid", entityFilter.getWorkingStationId());
		SDPRequest sdpRequest = (SDPRequest) query.getResultList().get(0);
		return sdpRequest;
	}

	/**
	 * Take the list of SDP Requests using the last inserted SDP Request as filter.
	 * @param entityFilter Entity SDP Request uses as filter.
	 * @return List The List of SDP Requests.
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public List<SDPRequest> loadLastCounter(SDPRequest entityFilter) {
    	Properties p = loadQueriesSQL.getProperties();
		String querySQL = p.getProperty("loadSDPRequestLastCounter");
		Query query = getEntityManager().createNativeQuery(querySQL, SDPRequest.class);
		query.setParameter("ACCOUNTINGDATE", entityFilter.getAccountingDate());
		query.setParameter("branch", entityFilter.getBranch());
		query.setParameter("numberoperationpdl", entityFilter.getNumberOperationPdl());
		query.setParameter("workingstationid", entityFilter.getWorkingStationId());
		List<SDPRequest> sdpRequest = query.getResultList();
		return sdpRequest;
	}

}
